Skip to content

console: live runtime events and guided bulk re-login - #603

Open
guyu233 wants to merge 5 commits into
caigee-cmd:mainfrom
guyu233:upstream/console-live
Open

guyu233 wants to merge 5 commits into
caigee-cmd:mainfrom
guyu233:upstream/console-live

Conversation

@guyu233

@guyu233 guyu233 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Change

Makes the console show what the server is actually doing instead of a locally invented animation, and turns the account batch actions into one guided flow.

  • Real event channel: /api/events (SSE) streams runtime state changes from internal/events; the overview in-flight card and account cards subscribe through useAccountEvents and drop the mock inflight generator.
  • Truthful bulk operations: check-in / probe / quota / maintenance fan out concurrently in the runtime and report per-account results over the event channel, instead of the UI faking progress.
  • Bulk re-login walkthrough: select accounts, open their login pages one at a time, sign in, and the console advances automatically; progress, waiting, timeout and cancel states are explicit (BatchReloginModal).
  • Checkbox fix: the re-login account checkbox is actually clickable now (HeroUI checkbox isSelected/onChange wiring).

Scope to review

  • Console/UI and runtime event notifications only. No changes to request routing, retries, provider cooldowns or billing.
  • The runtime now publishes account/maintenance state events; the event bus is in-process and best-effort (a slow subscriber cannot block a request).
  • Batch operations still run inside the existing concurrency guards; this only removes the artificial per-account serialization in the console handlers.

Verification

  • go test ./... passes.
  • go build ./... passes.
  • cd frontend && npm run sync rebuilt internal/webui/static.
  • Manual: overview in-flight numbers track real requests, account cards update without polling, re-login advances through selected accounts.

… concurrent

- Check-in all and refresh credits fan out to every account in one pass: the new
  bulk endpoints return one result per account with the verbatim upstream error,
  and the console no longer batches or queues requests.
- GET /api/events streams the runtime event bus (account / accounts / quota /
  checkin / refresh / bulk) with sequence replay. Console pages patch rows live
  and fall back to a 5s poll when the stream is unavailable.
- Check-in results carry the real reward credits plus code and HTTP status, and a
  provider that reports no status stays "unknown" instead of "success".
- Quota is persisted as the real upstream snapshot; force-ready overrides only
  enter the routing pool and the console badges them. Each quota snapshot states
  its age, and the synthetic inflight demo board plus its login showcase are gone.
- Video, audio and file content parts are refused with a real 400 invalid_request
  instead of being dropped during prompt assembly, and a shared head timeout makes
  Trae streams fail over when the upstream accepts a request and stays silent.
Select account cards, confirm, and the console authorizes them one at a time: start the provider login, show the authorization page, poll status, and advance automatically. Cancel stops the walkthrough; every row keeps its own state and the verbatim upstream message.

@caigee-cmd caigee-cmd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Console and runtime notifications only: /api/events is behind the console key, the bus is in-process and drops a slow subscriber instead of blocking a request, and the in-flight card now follows real requests instead of the local mock. Bulk check-in, probe, quota and re-login report per-account results. No change to routing, retries, cooldowns or billing.

@caigee-cmd

Copy link
Copy Markdown
Owner

合入。只动控制台和运行时通知:/api/events 走控制台密钥,事件总线在进程内,慢订阅者会被丢掉而不是堵住请求;概览的在途卡片改为跟随真实请求,不再用本地模拟。批量签到、探测、额度刷新和重新登录按账号回报结果。路由、重试、冷却和计费都没动。

@caigee-cmd

Copy link
Copy Markdown
Owner

审完了,可以合。/api/events 走控制台密钥,事件总线在进程内,慢订阅者会被丢掉而不是堵住请求;概览的在途卡片跟随真实请求,批量签到、探测、额度刷新和重新登录按账号回报结果。路由、重试、冷却和计费都没动。

现在合不进去:#602 合入后,这支在 frontend/src/i18n/messages.ts 和 internal/webui/static/index.html 上冲突。麻烦把 main 合进来、重新跑一次 cd frontend && npm run sync,我看到 CI 绿了就合。

@caigee-cmd caigee-cmd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed against current main. The console half is what I approved: /api/events is behind withConsoleKey, the bus drops a slow subscriber instead of blocking a publish, and the inflight card follows real requests. That part can land.

The branch is merge-dirty (base is ed63b9be, now well behind main) and it is not console-only. Please split these out before rebasing:

  • rejectUnsupportedMedia in ValidateChatRequest turns video / audio / file parts into a hard 400. Today those parts are dropped during prompt assembly. That is a gateway behavior change on every provider, not a console fix. It needs its own PR and a decision per provider — Qoder and Trae do not both have the same media story.
  • providers.DoWithHeadTimeout on Trae chat (default 60s, TRAE_STREAM_TTFB_MS) is a second stream clock next to the 5-minute idle guard in #601. Pick one owner. A silent Trae head is worth failing over, but it belongs with the stream-lifetime work, not with the SSE UI.
  • manager_maintenance.go (+225/−128) overlaps #601's check-in rewrite. Rebase will conflict there; don't resolve it by taking both.

refresh-all and the bulk re-login walkthrough look fine once the branch is console-only again. Checkbox fix too.

@caigee-cmd

Copy link
Copy Markdown
Owner

需要改什么

控制台这一半可以留,我也已经 approve 过:/api/events 走 withConsoleKey,慢订阅者被关掉而不是堵住 Publish,overview 的 in-flight 跟真实请求走,批量签到/探测/配额把每个账号的结果推出来,批量重登按账号推进。这些不要重写。

这支现在合不进去,而且不全是控制台。base 是 ed63b9be,mergeable 是 dirty。请先把下面三块拆出去,再 rebase 到今天的 main。rebase 之后 manager_maintenance.go 会和 #601 撞上,不要在冲突里把两边的签到改动一起留下。

1. 媒体拒绝从这支拿掉

internal/translate/compat.go 的 ValidateChatRequest 增加了 rejectUnsupportedMedia。video_url / input_video / video / input_audio / audio_url / audio / input_file / file 现在直接 400。

今天 main 的行为是这些 part 在组 prompt 时被丢掉,请求仍然发出去。改成 400 是所有 provider 的网关行为变化,不是控制台修复。Qoder 和 Trae 对媒体的处理不一样,不能在通用校验里一刀切。

请从这支删除:

  • rejectUnsupportedMedia 以及 ValidateChatRequest 里对它的调用
  • internal/translate/media_test.go
  • internal/providers/qoder/direct.go 里新增的那次 translate.ValidateChatRequest(&req)。Qoder direct 现在不走这条校验;在这支里加一次,等于把上面的 400 再专门绑到 Qoder 上

媒体要改的话另开一支,按 provider 写:哪一家会静默丢、哪一家上游真的收 image 以外的 part。在那之前保持“丢掉但请求成功”。

2. Trae 首包超时从这支拿掉

internal/providers/httptimeout.go 整文件,以及 internal/providers/trae/client.go 里两处:

resp, err := providers.DoWithHeadTimeout(client, httpReq, providers.HeadTimeoutFromEnv(traeStreamHeadTimeoutEnv, providers.DefaultStreamHeadTimeout))

ChatNonStream 和 ChatStream 都换了。默认 60 秒,TRAE_STREAM_TTFB_MS 可以改,但低于 5 秒会被抬回去。

这是流的寿命,不是 UI。#601 里还有一个 5 分钟的空闲 guard(只在 Read 堵住时计时)。两个都要的话必须同一支说明清楚:

  • 头还没到:60 秒,ErrFirstByteTimeout,failover
  • 头到了之后没有字节:5 分钟,ErrStreamIdle,failover
  • 客户端自己取消:仍然是 context.Canceled,不冷却账号

DoWithHeadTimeout 在头成功之后把 cancel 留到 body Close,这是对的,因为 Go 的 body 绑在请求 context 上,头一到就 cancel 会把已经开始的流截断。拆出去的时候这个语义要带着走,不要改成 context.WithTimeout 包住整个 Do。

这支里不要留 httptimeout.go,也不要改 trae/client.go。

3. 签到/维护运行时不要在这支里改调度

internal/runtime/manager_maintenance.go 这支是 +225/−128。#601 也在改同一个文件:它把学到的上游窗口换成了运营设置(默认 09:00)。两支一起 rebase 时,冲突解法不是两边都要。

这支允许留下的是“把已经发生的结果推到 event bus”:

  • 一次真实的 quota 落库 → TypeQuota
  • 一次真实的 check-in 结果 → TypeCheckin
  • 批量任务的 start / 每个账号 / done → TypeBulk

不允许在这支里改的是“什么时候去领、失败了会不会重试、自动时刻从哪来”。那些留在 #601 的讨论里。internal/runtime/manager_probe.go、manager_quota.go 如果只是为了发事件而改了探测顺序或并发,请把并发改动也拆出去,事件用现在的串行结果照样能发。

internal/accounts/quota.go 和 types.go 里如果只是给事件加了展示字段(奖励额度、快照年龄、force-ready 标记),可以留。如果 force-ready 会让一个上游报错的账号重新进路由池,那就不是控制台改动,请指出是哪一行把 Ready 写回 true,并把它移出这支。PR 正文说 force-ready 只进路由池、控制台另外打标。这个行为要有测试:打标的账号在配额接口仍然显示上游的失败状态,而不是被改写成成功。

4. 控制台这一半 rebase 时保持这些不变量

internal/events/events.go:

  • Publish 全程持有 mu。现在 fan-out 是在锁里对每个 subscriber 做一次非阻塞 send,慢的就 close。这个可以留,订阅者数量是控制台连接数,不是请求数。不要改成在锁里同步等待。
  • Subscribe 先把 ch 放进 subs,再在同一把锁里把 ring 里 Seq > lastSeq 的事件放进 ch。ring 是 1024,subBuffer 是 ringSize+64,所以回放不会堵在 mu 上。请保持这个容量关系。有人把 buffer 改小的话,Subscribe 会在持锁时永久堵在 ch <- event。
  • 慢消费者被 close(ch) 之后,HandleEvents 收到 ok == false 就返回。客户端用 Last-Event-ID 重连。不要在服务器端把被关掉的 ch 再塞回去,否则一个不读的连接会一直占着 map。

internal/console/events.go:

  • 路由继续是 s.withConsoleKey(...)。不要为了 EventSource 不能自定义 header 而改成 query token。现在的控制台 key 如果是 header,EventSource 需要走 fetch + ReadableStream,或者 cookie。不要把 console key 放到 ?access_token= 里,那会进代理日志。
  • Last-Event-ID 和 ?last_seq= 都可以。非法值现在被忽略成 0,等于把整个 ring 再推一遍。请改成:解析失败就当“只要直播、不回放”(从当前 seq 开始),不要把 1024 条历史再发给一个写错头的客户端。
  • 心跳是注释帧 : keepalive,15 秒。不要改成 data 事件,否则前端会把它当成状态更新。

internal/server/compress 那一层(在 #604)已经把 /api/events 排除出 gzip。rebase 之后请确认这支的路由名仍然是 /api/events。改名的话 #604 的排除列表要一起改,否则 SSE 会被 gzip 住,Flush 之前客户端什么都看不到。

前端:

  • useAccountEvents 断线之后退回 5 秒轮询。这个回退要留,SSE 不是唯一数据源。
  • 删掉本地伪造的 inflight 动画(liveInflight.ts 那次大删)可以留。不要在 rebase 时把 mock 生成器加回来。
  • 批量重登的 checkbox 继续用 HeroUI 的 isSelected / onChange。不要改回原生 input 上叠一层挡住点击的 label。
  • 文案走 frontend/src/i18n/messages.ts 的中英两套。rebase 之后跑 cd frontend && npm run sync,让 internal/webui/static 和 frontend/dist 一致。只提交这次 sync 产生的 hash,不要手工改静态 JS。

5. 拆完之后的验收

rebase 到当前 main 之后:

  • go test ./internal/events ./internal/console ./internal/runtime 过
  • cd frontend && npm run sync 之后 git diff --stat internal/webui/static 里只有这支 UI 改动对应的资源
  • diff 里不再出现 internal/translate/compat.go、internal/providers/httptimeout.go、internal/providers/trae/client.go、internal/providers/qoder/direct.go
  • 手动:开着 overview 发一个真实请求,in-flight 数字跟着变;断掉 /api/events 后 5 秒轮询仍然更新;批量重登能取消,取消后不再去拉下一个账号的登录页

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants